import { ref } from 'vue'
import { defineStore } from 'pinia'

export interface Assignment {
  id: number
  title: string
  description: string
  difficulty: 'easy' | 'medium' | 'hard'
  estimatedTime: string
}

export interface Example {
  id: string
  title: string
  description: string
  content: string
  filePath: string
  fileName?: string
}

export interface ReturnRoute {
  path: string
  query?: Record<string, any>
}

export type SourceType = 'assignment' | 'chapter2-examples' | 'chapter3-examples' | 'chapter4-examples' | 'examples' | 'assignments'

export const useAssignmentStore = defineStore('assignment', () => {
  const currentAssignment = ref<Assignment | null>(null)
  const currentExample = ref<Example | null>(null)
  const sourceType = ref<SourceType>('assignment')
  const returnRoute = ref<ReturnRoute | null>(null)

  function setCurrentAssignment(assignment: Assignment, preserveSourceType: boolean = false) {
    currentAssignment.value = assignment
    if (!preserveSourceType) {
      sourceType.value = 'assignment'
    }
  }

  function clearCurrentAssignment() {
    currentAssignment.value = null
    sourceType.value = 'assignment'
    returnRoute.value = null
  }

  function setCurrentExample(example: Example, source: SourceType = 'examples', fromRoute?: ReturnRoute) {
    currentExample.value = example
    sourceType.value = source
    if (fromRoute) {
      returnRoute.value = fromRoute
    }
  }

  function clearCurrentExample() {
    currentExample.value = null
    sourceType.value = 'assignment'
    returnRoute.value = null
  }

  function setReturnRoute(route: ReturnRoute) {
    returnRoute.value = route
  }

  function clearReturnRoute() {
    returnRoute.value = null
  }

  return {
    currentAssignment,
    currentExample,
    sourceType,
    returnRoute,
    setCurrentAssignment,
    clearCurrentAssignment,
    setCurrentExample,
    clearCurrentExample,
    setReturnRoute,
    clearReturnRoute
  }
})